package controller;

import business.ebi.CommodityEBI;
import business.factory.BusinessFactory;
import entity.Commodity;
import entity.ShoppingCart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "CommodityListController",urlPatterns = "/CommodityListController")
public class CommodityListController extends HttpServlet {

    private final CommodityEBI ebi = BusinessFactory.getCommodityEBI();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("method");
        if (action!=null){
        try {
            Method method =this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,req,resp);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }
        }else {
            this.doPost(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Commodity> all = null;
        try {
            all = ebi.findAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        req.setAttribute("Commodity",all);
        req.getRequestDispatcher("/CommodityList.jsp").forward(req,resp);
    }

    protected void toAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setAttribute("method","add");
        resp.sendRedirect(req.getContextPath()+"/Add.jsp?method=toAdd");
    }

    protected void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        Commodity commodity = ebi.search_id(req.getParameter("cId"));
        req.setAttribute("commodity", commodity);
        req.getRequestDispatcher("/Add.jsp?method=toUpdate").forward(req, resp);
    }

    protected void toDelete(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        String cId = req.getParameter("cId");
        ebi.delete(cId);
        req.getRequestDispatcher("/CommodityListController?method=doPost").forward(req, resp);
    }

    protected void toShoppingCar(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        String cId = req.getParameter("cId");
        Commodity commodity = ebi.search_id(cId);
        req.setAttribute("shop_commodity", commodity);
        req.getRequestDispatcher("/CommodityDetail.jsp").forward(req, resp);
    }

    protected void toCart(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        resp.setContentType("text/html;charset=utf8");
        HttpSession session = req.getSession();
        ShoppingCart shoppingCart = (ShoppingCart) session.getAttribute("shoppingcart");
        if (shoppingCart == null || shoppingCart.getMap().isEmpty()) {
            resp.getWriter().write("<script language=javascript>alert('" + "购物车目前无商品" + "');window.history.back()</script>");
        } else {
            req.getRequestDispatcher("/ShoppingCar.jsp").forward(req, resp);
        }
    }

    protected void search(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        resp.setContentType("text/html;charset=utf8");
        String key = req.getParameter("key");
        String searchMethod = req.getParameter("searchMethod");
        List<Commodity> list = new ArrayList<>();
        if (searchMethod.equals("商品条形码")) {
            Commodity commodity = ebi.search_id(key);
            list.add(commodity);
        } else if (searchMethod.equals("商品名称")) {
            list = ebi.search_name(key);
        }
        if (list.isEmpty() || list.get(0) == null) {
            resp.getWriter().write("<script language=javascript>alert('" + "查询不到该商品" + "');window.history.back()</script>");
        } else {
            req.setAttribute("Commodity", list);
            req.getRequestDispatcher("/CommodityList.jsp").forward(req, resp);
        }
    }
}
